package com.example.grape_effect.animation

import android.opengl.GLES20
import com.example.grape_effect.gl.common.GLProgram
import com.example.grape_effect.gl.common.ShaderConstant
import com.example.grape_effect.gl.common.ShaderParameter
import com.example.grape_effect.gl.common.Texture
import com.example.grape_effect.gl.glutils.Matrix2DUtils
import com.example.grape_effect.base.GrapeEffect

 open class LYAnimation: GLProgram() {
    companion object{
        const val invalidTimeValue = -1L
        // time mode，draw的时候设置为开始时间，后续会根据duration计算progress和当前位置，可看方法 updateCurPositionByTime
        const val ANIMATION_MODE_BY_SET_TIME_SELF = 0
        // set progress 外面调用者自己设置progress，可以看 updateCurPosition 的用法，外界设置progress以及起始点和终点
        const val ANIMATION_MODE_BY_SET_TIME = 1
    }
    var duration = 1500L
    var startTime = -1L
    var isRepeatable = true
    var mode = ANIMATION_MODE_BY_SET_TIME_SELF

    override var vertexShader =// ShaderConstant.SHOW_FILTER_VERTEX_SHADER
        """
            attribute vec4 vPosition;
            attribute vec2 inputTextureCoordinate;
            varying vec2 textureCoordinate;
            uniform mat4 matrix;
            void main()
            {
                gl_Position = matrix*vec4(vPosition.x, vPosition.y, 0.0, 1.0);
                textureCoordinate = vec2(inputTextureCoordinate.x, 1.0 - inputTextureCoordinate.y);
            }
        """.trimIndent()
    override var fragmentShader = ShaderConstant.NO_FILTER_FRAGMENT_SHADER

    override fun addParameters() {
        parameters["vPosition"] = ShaderParameter(
            "vPosition",
            ShaderParameter.TYPE_ATTRIBUTE,
            ShaderParameter.VALUE_TYPE_ATTRIBUTE_PARAMETER
        )
        parameters["inputTextureCoordinate"] = ShaderParameter(
            "inputTextureCoordinate",
            ShaderParameter.TYPE_ATTRIBUTE,
            ShaderParameter.VALUE_TYPE_ATTRIBUTE_PARAMETER
        )
        parameters["inputTexture"] = ShaderParameter(
            "inputTexture",
            ShaderParameter.TYPE_TEXTURE,
            ShaderParameter.VALUE_TYPE_IMAGE_TEXTURE
        )
        parameters["matrix"] = ShaderParameter(
            "matrix",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT_MATRIX
        )
    }

    override fun setDefaultParameterValues() {
        parameters["vPosition"]?.value =
            ShaderParameter.AttributeParameter(ShaderConstant.POSITIONS, 2)
        parameters["inputTextureCoordinate"]?.value = ShaderParameter.AttributeParameter(
            ShaderConstant.COORDINATE, 2
        )
        parameters["inputTexture"]?.value = GrapeEffect.testInTexture
        parameters["matrix"]?.value = Matrix2DUtils.getIdentityMatrix()
    }

     fun setMatrix(matrix: FloatArray) {
        parameters["matrix"]?.value = matrix
    }
    open fun setInTexture(texture: Texture){
        parameters["inputTexture"]?.value = texture
    }

    open fun getInTexture(): Texture {
        return parameters["inputTexture"]?.value as Texture
    }

    // 主要用来获取mvp矩阵参数，再利用setMatrix更新矩阵
    open fun updateAnimationParameters(){

    }

    override fun draw() {
        if (mode == ANIMATION_MODE_BY_SET_TIME_SELF && startTime == invalidTimeValue) {
            startTime = System.currentTimeMillis()
        }
        initProgram()
        GLES20.glUseProgram(program)
        updateAnimationParameters()
        setParametersToShader()
        GLES20.glEnableVertexAttribArray(parameters["vPosition"]!!.location)
        GLES20.glEnableVertexAttribArray(parameters["inputTextureCoordinate"]!!.location)
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
        GLES20.glDisableVertexAttribArray(parameters["vPosition"]!!.location)
        GLES20.glDisableVertexAttribArray(parameters["inputTextureCoordinate"]!!.location)
    }

}